package 罗马数字转整数;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();

//        String input = "III";
        String input = "MCMXCIV";

        System.out.println(solution.romanToInt(input));
    }


    public int romanToInt(String s) {

        int result = 0;

        int[] numbers = new int[]{1,5,10,50,100,500,1000};
//        String[] types = new String[]{"I","V","X","L","C","D","M"};

        List<Character> types = Arrays.asList('I','V','X','L','C','D','M');
        List<Integer> indexs = new ArrayList<>();

        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            int index = types.indexOf(chars[i]);
            indexs.add(index);
        }

        for (int i = 0; i < indexs.size(); i++) {
            if(i != indexs.size() - 1){
                if(indexs.get(i) < indexs.get(i+1)){
                    result += numbers[indexs.get(i+1)] - numbers[indexs.get(i)];
                    i++;
                    continue;
                }
            }
            result += numbers[indexs.get(i)];
            continue;
        }

        return result;
    }

}
